package com.qf.dao.impl;

import com.qf.bean.Zhanghu;
import com.qf.dao.ZhangHuDao;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 2019/11/15
 * Administrator
 * spring_anno_aop_1113
 * 面向对象面向君  不负代码不负卿
 */
public class ZhangHuDaoImpl extends JdbcDaoSupport implements ZhangHuDao {
    public Zhanghu findbyid(int id) {
        Zhanghu zh= null;
        try {
            zh = getJdbcTemplate().queryForObject("select * from zhanghu where uid=?", new RowMapper<Zhanghu>() {
                public Zhanghu mapRow(ResultSet resultSet, int i) throws SQLException {
                    Zhanghu zhanghu = new Zhanghu();
                    zhanghu.setYue(resultSet.getInt("yue"));
                    return zhanghu;
                }
            }, id);
        } catch (DataAccessException e) {
            return null;
        }
        return zh;
    }

    public int geiqian(int id, int money) {
        int update = getJdbcTemplate().update("update zhanghu set yue=yue-? where uid=?", money, id);
        return update;
    }

    public int shouqian(int id, int money) {
        int update = getJdbcTemplate().update("update zhanghu set yue=yue+? where uid=?", money, id);
        return update;
    }

    public int inserta() {
        return getJdbcTemplate().update("insert into zhanghu(uname,yue) values(?,?)","刘能",1000);
    }

    public List<Zhanghu> finda() {
        return getJdbcTemplate().query("select * from zhanghu", new RowMapper<Zhanghu>() {
            public Zhanghu mapRow(ResultSet resultSet, int i) throws SQLException {
                Zhanghu zhanghu = new Zhanghu();
                zhanghu.setYue(resultSet.getInt("yue"));
                zhanghu.setUname(resultSet.getString("uname"));
                zhanghu.setUid(resultSet.getInt("uid"));
                return zhanghu;
            }
        });
    }


    public List findb() {
        return getJdbcTemplate().query("select * from zhanghu", new RowMapper<Zhanghu>() {
            public Zhanghu mapRow(ResultSet resultSet, int i) throws SQLException {
                Zhanghu zhanghu = new Zhanghu();
                zhanghu.setYue(resultSet.getInt("yue"));
                zhanghu.setUname(resultSet.getString("uname"));
                zhanghu.setUid(resultSet.getInt("uid"));
                return zhanghu;
            }
        });
    }

    public int updateb() {
        return getJdbcTemplate().update("update zhanghu set yue=8888 where uid=1");
    }
}
